package com.example.hangzhou.welbeing.mytestdemoapp.base;

import android.app.Application;
import android.content.Context;

import com.mob.MobSDK;
import com.taobao.sophix.SophixManager;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import cn.jesse.nativelogger.NLogger;
import cn.jesse.nativelogger.formatter.SimpleFormatter;
import cn.jesse.nativelogger.logger.LoggerLevel;
import cn.jesse.nativelogger.util.CrashWatcher;
import io.realm.Realm;

/**
 * 杭州惟君医疗科技有限公司11
 * Created by 20433 on 2018/2/24
 */

public class MyApplcation extends Application {


    public static Context getMcontext() {
        return mcontext;
    }

    private static Context mcontext;

    @Override
    public void onCreate() {
        super.onCreate();
        // todo 热修复主动拉取补丁
        // queryAndLoadNewPatch不可放在attachBaseContext 中，否则无网络权限，建议放在后面任意时刻，如onCreate中
        SophixManager.getInstance().queryAndLoadNewPatch();

        NLogger.getInstance()
                .builder()
                .tag("APP")
                .loggerLevel(LoggerLevel.DEBUG)
                .fileLogger(true)
                .fileDirectory(getApplicationContext().getFilesDir().getPath() + "/logs")
                .fileFormatter(new SimpleFormatter())
                .expiredPeriod(3)
                .catchException(true, new CrashWatcher.UncaughtExceptionListener() {
                    @Override
                    public void uncaughtException(Thread thread, Throwable ex) {
                        NLogger.e("uncaughtException", ex);
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                })
                .build();

        mcontext =  getApplicationContext();
        initSDK();
    }

    private void initSDK() {
// 通过代码注册你的AppKey和AppSecret
        MobSDK.init(getMcontext(), "24965088edcd2", "fd826457eb4d45ff2a056def60b704a9");


//    todo     友盟统计初始化
        /**
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:友盟 app key
         * 参数3:友盟 channel
         * 参数4:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数5:Push推送业务的secret
         */
        UMConfigure.init(this, null, null, UMConfigure.DEVICE_TYPE_PHONE,null);
        MobclickAgent.openActivityDurationTrack(false);
        UMConfigure.setLogEnabled(true);

        Realm.init(this);
    }
}
